
package com.means.polymerize.common.util;

import com.means.polymerize.common.constants.Global;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicReference;

/**
 * The type Home util.
 *
 * @author xuhaifeng
 * @date 2021.05.18
 */
@Component
public class HomeUtil {
    private static final String WINDOWS = "Windows";
    /**
     * 应用部署目录
     */
    private static final AtomicReference<String> APPLICATION_HOME = new AtomicReference<>();

    static {
        final AtomicReference<String> absolutePath = new AtomicReference<>(Global.NOT);
        final ApplicationHome applicationHome = new ApplicationHome(HomeUtil.class);
        final File source = applicationHome.getSource();
        if (Objects.nonNull(source)) {
            absolutePath.set(source.getParentFile().getAbsolutePath());
        } else {
            absolutePath.set(applicationHome.getDir().getParentFile().getAbsolutePath());
        }
        APPLICATION_HOME.set(String.format("%s%s", absolutePath.get(), File.separator));
    }

    /**
     * Gets application home.
     *
     * @return the application home
     */
    @SuppressWarnings("unused")
    public static String getApplicationHome() {
        return APPLICATION_HOME.get();
    }

}
